package io.tan.searchestest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.detabes.time.core.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author lxw
 * @version V1.0
 * @date 2021/8/3
 **/
@Slf4j
public class tes {
	public static void main(String[] args) throws Exception {
		String string = "http://127.0.0.1:9000/12312/4/20211110144515362.png";

		System.out.println(string.startsWith("http://127.0.0.1:9000/12312/"));

		System.out.println(string.replaceFirst("http://127.0.0.1:9000/12312/",""));
//			String startZeroStr = String.format("%04d",111);
//		System.out.println("前补零方法二："+startZeroStr);

//		System.out.println(LocalDate.now().toString());
//		System.out.println(TimeUtil.getDatePoor2Number(new Date(), DateTime.now().plusDays(3).toDate(),1));

//		String kewWord = "  (K=(CAD OR CAM) OR T=雷达)  AND R=机械 NOT K=模具 AND (I=张三机构 OR M=测试)";
//		// 1. 多个空格替换为一个空格
//		kewWord = kewWord.trim().replaceAll("\\s+", " ");
//		System.out.println(kewWord);
//
//		// 1 先按括号拆分
//		String regEx = "[()]+";
//		Pattern pattern = Pattern.compile(regEx);
//		String[] strs = pattern.split(kewWord);
//		System.out.println(JSON.toJSONString(strs));
//
//		String [] out = StringUtils.substringsBetween(kewWord,"(", ")");
//		System.out.println(JSON.toJSONString(out));
//
//		String [] outs = kewWord.split("[\\(\\)]");
//		System.out.println(JSON.toJSONString(outs));
//		List<String> list = new ArrayList<>();
//		list.add("1");
//		list.add("2");
//		list.add("3");
//		list.add("4");
//		System.out.println(list.stream().collect(Collectors.joining("\',\'")));

//		final String text = "用户名称是爱新觉罗马";
//		final byte[] textByte = text.getBytes("UTF-8");
//编码
////		final String encodedText = encoder.encodeToString(textByte);
//		String encodedText = Base64.getUrlEncoder().encodeToString(textByte);
//		System.out.println(encodedText);
////解码
//		System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

//		String arr = " asd asd ";
//		System.out.println(arr.trim());

//		Date date = DateUtils.parseDate("2021-08-05 00:00:00", "yyyy-MM-dd HH:mm:ss");
//		Date nowDate = new Date();
//		String format1 = DateFormatUtils.format(date, "yyyy-MM-dd");
//		System.out.println(format1);
//		nowDate = DateUtils.setHours(nowDate, 0);
//		nowDate = DateUtils.setMinutes(nowDate, 0);
//		nowDate = DateUtils.setSeconds(nowDate, 0);
//		nowDate = DateUtils.setMilliseconds(nowDate, 0);
//		long diff = date.getTime() - nowDate.getTime();
//		System.out.println(diff > 0);
//		Date date = new Date();
//		date = DateUtils.setHours(date,0);
//		date = DateUtils.setMinutes(date,0);
//		date = DateUtils.setSeconds(date,0);
//		date = DateUtils.setMilliseconds(date,0);
//		System.out.println(date.getTime());
//		String format1 = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
//		System.out.println(format1);
//		String format1 = DateFormatUtils.format(, "yyyy-MM-dd");
//		Date date = DateUtils.parseDate("2021-08-05 00:00:00", "yyyy-MM-dd HH:mm:ss");
//		System.out.println(date);
//		long l = date.getTime();
//		System.out.println(l);
//		String s = String.valueOf(l);
//		System.out.println(s.substring(0,s.length()-3));

//		String format = DateFormatUtils.format(l, "yyyy-MM-dd");
//		System.out.println(format);

//		String v = "1626710400";
//		String format1 = DateFormatUtils.format(Long.valueOf(v), "yyyy-MM-dd");
//		System.out.println(format1);
//		if(v.length()==10){
//			v+="000";
//		}
//		System.out.println(v);
//		String format = DateFormatUtils.format(Long.valueOf(v), "yyyy-MM-dd");
//		System.out.println(format);
//
//		Date date = new Date();
//		System.out.println(date.toString());
//		String str = "13812345678";
//		System.out.println(String.format("用户%s",str.substring(8)));
//		System.out.println(isConnect("https://thirdwx.qlogo.cn/mmopen/vi_32/gHUVCVKq0Zf3qZ1JVAsY1O89vvtdHAxpbmU5he8vM9eCtl8pV2eX3iadUDVhJgfdz0UamkFiaW2UN11tIIu4OarA/132"));
	}


	private static URL url;

	private static HttpURLConnection con;

	private static int state = -1;
	private static int count = 5;

	/**
	 * 功能：检测当前URL是否可连接或是否有效,
	 * <p>
	 * 描述：最多连接网络 5 次, 如果 5 次都不成功，视为该地址不可用
	 *
	 * @param urlStr 指定URL网络地址
	 * @return URL
	 */
	public static boolean isConnect(String urlStr) {
		int counts = 0;
		if (urlStr == null || urlStr.length() <= 0) {
			return false;
		}
		while (counts < count) {
			try {
				url = new URL(urlStr);
				con = (HttpURLConnection) url.openConnection();
				state = con.getResponseCode();
				System.out.println(counts + "= " + state);
				if (state == 200) {
					log.info("URL可用！");
				}
				return true;
			} catch (Exception ex) {
				counts++;
				log.info("URL不可用，连接第 " + counts + " 次");
				url = null;
				continue;
			}
		}
		return false;
	}
}
